package com.sfh.appversion.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sfh.appversion.base.exception.ServiceException;
import com.sfh.appversion.dao.entity.PhoneTypeDO;
import com.sfh.appversion.dao.entity.SysConfigDO;
import com.sfh.appversion.dao.entity.UserDO;
import com.sfh.appversion.dao.mapper.PhoneTypeMapper;
import com.sfh.appversion.dao.mapper.SysConfigMapper;
import com.sfh.appversion.dao.mapper.UserMapper;
import com.sfh.appversion.dto.req.*;
import com.sfh.appversion.dto.resp.UserRespDTO;
import com.sfh.appversion.server.UserServer;
import com.sfh.appversion.server.error.UserErrorCodeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserServerImpl implements UserServer {

    private final UserMapper userMapper;

    private final SysConfigMapper sysConfigMapper;

    private final PhoneTypeMapper phoneTypeMapper;

    @Override
    public UserRespDTO login(UseReqDTO req) {

        LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class)
                .eq(UserDO::getUserName, req.getLoginName())
                .eq(UserDO::getUserPassword, req.getLoginPassword());

        UserDO userDO = userMapper.selectOne(queryWrapper);
        if (userDO == null) {
            throw new ServiceException(UserErrorCodeEnum.USER_NOEXITS_ERROR);
        }
        UserRespDTO userRespDTO = UserRespDTO.create(userDO);

        return userRespDTO;
    }

    @Override
    public List<UserDO> queryList() {
        return userMapper.selectList(null);
    }

    @Override
    public boolean delUser(String userId) {
        return userMapper.deleteById(userId) > 0;
    }

    @Override
    public boolean addUser(UseNewReqDTO req) {
        LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class)
                .eq(UserDO::getUserName, req.getUserName());

        if (userMapper.exists(queryWrapper)) {
            throw new ServiceException(UserErrorCodeEnum.USER_EXITS_ERROR);
        }

        UserDO userDO = UserDO.builder()
                .userName(req.getUserName())
                .userPassword(req.getUserPassword())
                .userType("0")
                .build();
        return userMapper.insert(userDO) > 0;
    }

    @Override
    public List<SysConfigDO> queryAllConfig() {

        return sysConfigMapper.selectList(null);
    }

    @Override
    public SysConfigDO queryConfig(String configKey) {
        LambdaQueryWrapper<SysConfigDO> queryWrapper = Wrappers.lambdaQuery(SysConfigDO.class)
                .eq(SysConfigDO::getConfigKey, configKey);
        return sysConfigMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean delConfig(String configId) {

        return sysConfigMapper.deleteById(configId) > 0;
    }

    @Override
    public boolean addConfig(SysConfingNewReqDTO req) {

        SysConfigDO sysConfigDO = SysConfigDO.builder()
                .configKey(req.getConfigKey())
                .configValue(req.getConfigValue())
                .configType(req.getConfigType()).build();

        return sysConfigMapper.insert(sysConfigDO) > 0;
    }

    @Override
    public boolean editConfig(SysConfingEditReqDTO req) {

        LambdaQueryWrapper<SysConfigDO> queryWrapper = Wrappers.lambdaQuery(SysConfigDO.class)
                .eq(SysConfigDO::getConfigId, req.getConfigId());

        SysConfigDO sysConfigDO = sysConfigMapper.selectOne(queryWrapper);
        if (sysConfigDO == null) {
            throw new ServiceException(UserErrorCodeEnum.CONFIGNOEXITS_ERROR);
        }
        sysConfigDO.setConfigType(req.getConfigType());
        sysConfigDO.setConfigValue(req.getConfigValue());
        return sysConfigMapper.updateById(sysConfigDO) > 0;
    }

    @Override
    public List<PhoneTypeDO> queryAllPhoneType() {
        return phoneTypeMapper.selectList(null);
    }

    @Override
    public List<PhoneTypeDO> queryPhoneType(String phoneType) {

        LambdaQueryWrapper<PhoneTypeDO> queryWrapper = Wrappers.lambdaQuery(PhoneTypeDO.class)
                .eq(PhoneTypeDO::getPhoneType, phoneType);
        return phoneTypeMapper.selectList(queryWrapper);
    }

    @Override
    public boolean delPhoneType(String phoneTypeId) {
        return phoneTypeMapper.deleteById(phoneTypeId) > 0;
    }

    @Override
    public boolean addPhoneType(PhoneTypeNewReqDTO req) {

        PhoneTypeDO phoneTypeDO = PhoneTypeDO.builder()
                .phoneType(req.getPhoneType())
                .phoneTypeBrand(req.getPhoneTypeBrand())
                .phoneTypeModel(req.getPhoneTypeModel())
                .phoneTypeVersion(req.getPhoneTypeVersion()).build();
        return phoneTypeMapper.insert(phoneTypeDO) > 0;
    }
}
